package com.tpc.control.bean;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.tpc.control.util.LdapUtil;

public class Con000 {
	private String userName;
	private String password;
	private Boolean logon = false;
	
	public Boolean getLogon() {
		return logon;
	}
	public void setLogon(Boolean logon) {
		this.logon = logon;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String loginUser() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		if ((this.getUserName().equals("")) || (this.getPassword().equals(""))) {
			FacesMessage facesMessage = new FacesMessage(
					"Please enter your Username and/or Password");
			facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
			facesContext.addMessage("con000Form", facesMessage);
			logon = false;
			return "con000";
		} else {
			try {
				if (LdapUtil.verifyPassword(this.getUserName(), this.getPassword())) {
					logon = true;
					return "con001";
				} else {
					logon = false;
					FacesMessage facesMessage = new FacesMessage(
					"Your Username and/or Password is incorrect");
					facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
					facesContext.addMessage("con000Form", facesMessage);
					return "con000";
				}
			} catch (Exception e) {
				logon = false;
				e.printStackTrace();
				FacesMessage facesMessage = new FacesMessage(e.toString());
				facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
				facesContext.addMessage("con000Form", facesMessage);
				return "con000";				
			}
		}
	}
	
	public String logoff() {
		FacesContext fc = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) fc.getExternalContext().getSession(
				false);
		session.invalidate();
		return "con000";
	}
}
